package com.finace.money.ui.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.content.LocalBroadcastManager;

import com.finace.money.R;
import com.finace.money.common.Constant;
import com.finace.money.common.util.ACache;
import com.finace.money.common.util.ACacheClearUtil;
import com.finace.money.common.util.StringUtil;
import com.finace.money.di.component.AppComponent;
import com.finace.money.net.socket.ConnService;
import com.finace.money.net.socket.PushReceiver;

/**
 * Created by zhangpeng on 2018/1/2.
 */

public class SplashActivity extends BaseActivity {
    @Override
    public int setLayout() {
        return R.layout.activity_splash;
    }

    @Override
    public void init() {

        Intent intent = new Intent(this, ConnService.class);
        startService(intent);
        ACacheClearUtil.loginOut();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(PushReceiver.ACTION_LIST);
        LocalBroadcastManager.getInstance(this).registerReceiver(listBroadcastReceiver, intentFilter);
        unregisterReceiver(listBroadcastReceiver);
    }

    public BroadcastReceiver listBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String finish = intent.getStringExtra(PushReceiver.FINISH);
            if (finish.equals(PushReceiver.FINISH)) {
                jump();
            }
        }
    };

    private void jump() {

        startActivity(new Intent(SplashActivity.this, MainActivity.class));
        String firstIn = ACache.get(this).getAsString(Constant.firstIn);
        if(StringUtil.isNullString(firstIn)){
            //导航结束后控制跳转标记
            Intent intent = new Intent(SplashActivity.this, GuideActivity.class);
            startActivity(intent);
        }else{
            Intent intent = new Intent(SplashActivity.this, MainActivity.class);
            startActivity(intent);
        }
        finish();
    }

    @Override
    public void setupAcitivtyComponent(AppComponent appComponent) {

    }

}
